package linuxbox.todo.logic;

import java.util.*;

import linuxbox.*;
import linuxbox.bean.*;
import linuxbox.todo.bean.*;
import linuxbox.util.*;

public class BoxTodo
{
  //XTODO: define in XML file
  public final static String[] Priority =
      {
      "Low", "Normal", "High"};

  //define the View Types
  public final static String VT_ALL = "ALL";
  public final static String VT_CURRENT = "CURRENT";
  public final static String VT_COMPLETED = "COMPLETED";
  public final static String VT_OVERDUE = "OVERDUE";

  public BoxTodo()
  {
  }

  public BoxTodoEntry get(BaseIntKey key)
  {
    BoxTodoEntry entry = null;
    try
    {
      BoxTodoEntryBean bean = new BoxTodoEntryBean(key);
      bean.load();
      entry = new BoxTodoEntry();
      entry.copyFrom(bean);
    }
    catch (Exception e)
    {
      //TODO: Log something?
      e.printStackTrace();
    }
    return entry;
  }

  public BoxTodoEntry add()
  {
    try
    {
      BoxTodoEntryBean bean = new BoxTodoEntryBean(new BaseIntKey(0));
      bean.create();

      BoxTodoEntry entry = new BoxTodoEntry();
      entry.copyFrom(bean);
      return entry;
    }
    catch (Exception e)
    {
      //XTODO: Log something?
      e.printStackTrace();
    }
    return null;
  }

  public boolean remove(BaseIntKey key)
  {
    try
    {
      BoxTodoEntryBean bean = new BoxTodoEntryBean(key);
      bean.remove();
    }
    catch (Exception e)
    {
      //TODO: Log something?
      e.printStackTrace();
      return false;
    }
    return true;
  }

  public boolean update(BoxTodoEntry entry)
  {
    try
    {
      BoxTodoEntryBean bean = new BoxTodoEntryBean(new BaseIntKey(entry.getID()));
      bean.copyFrom(entry);
//System.out.println(bean);
      bean.store();
    }
    catch (Exception e)
    {
      //TODO: Log something?
      e.printStackTrace();
      return false;
    }
    return true;
  }

  public int getTotalPages(int userid, int entriesPerPage, String VIEW_TYPE)
  {
    int pages = 1;
    try
    {
      StringBuffer sql = new StringBuffer();
      sql.append(" where userid =");
      sql.append(userid);

      if (VIEW_TYPE.compareTo(VT_CURRENT) == 0)
      {
        sql.append(" and complete = 0 ");
      }
      else if (VIEW_TYPE.compareTo(VT_COMPLETED) == 0)
      {
        sql.append(" and complete = 1 ");
      }
      else if (VIEW_TYPE.compareTo(VT_OVERDUE) == 0)
      {
        BoxStatic.dateTimeFormatter.applyPattern(BoxStatic.POSTGRES_DATE_FORMAT);
        String strDate = BoxStatic.dateTimeFormatter.format(new java.util.Date());
        sql.append(" and duedate < '");
        sql.append(strDate);
        sql.append(" and complete = 0 ");
      }

      BoxTodoEntryBean bean = new BoxTodoEntryBean(null, null);
      pages = ( (bean.getCountBySql(sql.toString()) - 1) / entriesPerPage) + 1;
    }
    catch (Exception e)
    {
      e.printStackTrace();
      //TODO: Log
    }
    return pages;
  }

  public Iterator getTodos(int userid, String VIEW_TYPE, String SORTBY,
                           boolean sortAsc, int from, int limit)
  {
    if (VT_CURRENT.compareTo(VIEW_TYPE) == 0)
    {
      return getCurrentTodos(userid, SORTBY, sortAsc, from, limit);
    }
    else if (VT_COMPLETED.compareTo(VIEW_TYPE) == 0)
    {
      return getCompletedTodos(userid, SORTBY, sortAsc, from, limit);
    }
    else if (VT_OVERDUE.compareTo(VIEW_TYPE) == 0)
    {
      return getOverdueTodos(userid, SORTBY, sortAsc, from, limit);
    }
    else
    {
      return getAllTodos(userid, SORTBY, sortAsc, from, limit);
    }
  }

  public Iterator getAllTodos(int userid, String SORTBY, boolean sortAsc,
                              int from, int limit)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" where userid ='");
    sql.append(userid);
    sql.append("' order by ");
    sql.append(SORTBY);
    if (sortAsc)
    {
      sql.append(" ASC ");
    }
    else
    {
      sql.append(" DESC ");

    }
    DBUtil.appendLimit(sql, from, limit);

    return getBySql(sql.toString());
  }

  public Iterator getOverdueTodos(int userid, String SORTBY, boolean sortAsc,
                                  int from, int limit)
  {
    BoxStatic.dateTimeFormatter.applyPattern(BoxStatic.POSTGRES_DATE_FORMAT);
    String strDate = BoxStatic.dateTimeFormatter.format(new java.util.Date());
    StringBuffer sql = new StringBuffer();
    sql.append(" where userid ='");
    sql.append(userid);
    sql.append("' and duedate < '");
    sql.append(strDate);
    sql.append("' and complete = 0 order by ");
    sql.append(SORTBY);
    if (sortAsc)
    {
      sql.append(" ASC ");
    }
    else
    {
      sql.append(" DESC ");

    }
    DBUtil.appendLimit(sql, from, limit);

    return getBySql(sql.toString());
  }

  public Iterator getCompletedTodos(int userid, String SORTBY, boolean sortAsc,
                                    int from, int limit)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" where userid ='");
    sql.append(userid);
    sql.append("' and complete = 1 order by ");
    sql.append(SORTBY);
    if (sortAsc)
    {
      sql.append(" ASC ");
    }
    else
    {
      sql.append(" DESC ");

    }
    DBUtil.appendLimit(sql, from, limit);

    return getBySql(sql.toString());
  }

  public Iterator getCurrentTodos(int userid, String SORTBY, boolean sortAsc,
                                  int from, int limit)
  {
    StringBuffer sql = new StringBuffer();
    sql.append(" where userid ='");
    sql.append(userid);
    sql.append("' and complete = 0 order by ");
    sql.append(SORTBY);
    if (sortAsc)
    {
      sql.append(" ASC ");
    }
    else
    {
      sql.append(" DESC ");

    }
    DBUtil.appendLimit(sql, from, limit);

    return getBySql(sql.toString());
  }

  public String getPriorityString(int priority)
  {
    return Priority[priority];
  }

  protected Iterator getBySql(String sql)
  {
    Vector v = new Vector();
    try
    {
      // Use the transaction, pass the DbAdapter to the bean
      DbAdapter db = new DbAdapter(true);
      BoxTodoEntryBean bean = new BoxTodoEntryBean(null, db);
      Iterator it = bean.findBySql(sql);
      while (it.hasNext())
      {
        BaseIntKey key = (BaseIntKey) it.next();
        bean = new BoxTodoEntryBean(key, db);
        bean.load();
        BoxTodoEntry entry = new BoxTodoEntry();
        entry.copyFrom(bean);
        v.add(entry);
      }
      db.close();
      db = null;
    }
    catch (Exception e)
    {
      e.printStackTrace();
      //TODO: Log
    }
    return v.iterator();
  }
}
